package com.darren.euc.util;



import java.util.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;




public class HMacUtil {
	
	private static final String HMACMD5 = "HmacMD5";
	private static final String HMACSHA256 = "HmacSHA256";
	private static final String SECURE_SECRET = "baa2277ae3bde436a40e2ac5f4ab72d5";
	
	/**
	 * encrypt  by algorithm, 
	 * if algorithmKey is null or empty,then set algorithm = 'HmacSHA256'
	 * if secretKey is null or empty,then set secret = default value
	 * @param message
	 * @param secretKey
	 * @param algorithmKey
	 * @return if message is null or empty, return null; else return encrypt string
	 */
	public static String encryptByAlgorithm(String message,String secretKey,String algorithmKey){
		String result = null;		
		String secret = StringUtil.isEmptyOrWhitespace(secretKey)?SECURE_SECRET:secretKey.trim();
		String algorithm = StringUtil.isEmptyOrWhitespace(algorithmKey)?HMACSHA256:algorithmKey.trim();
		if(!StringUtil.isEmptyOrWhitespace(message)){
			try {
				message = message.trim();
				Mac mac = Mac.getInstance(algorithm);
	            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), algorithm);
	            mac.init(secret_key);
	            byte[] macArray = mac.doFinal(message.getBytes("UTF-8"));
//	            System.out.println(HexUtil.toHexString(sha256Array));
	            result = base64Encoding(macArray);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}			
		}
//		System.out.println(String.format("message:%s; result:%s;",message, result));
        return result; 
	}
	
	
	public static String encryptHmacSHA256(String message,String secretKey){
        return encryptByAlgorithm(message,secretKey,HMACSHA256); 
	}

	public static String encryptHmacSHA256(String message){
		return encryptHmacSHA256(message,SECURE_SECRET);
	}
	
	public static String encryptHmacMD5(String message,String secretKey){
        return encryptByAlgorithm(message,secretKey,HMACMD5); 
	}

	public static String encryptHmacMD5(String message){
		return encryptHmacMD5(message,HMACMD5);
	}	
	
	private static String base64Encoding(byte[] inputByte){
		String result = null;
		if(inputByte != null && inputByte.length > 0){
			try {
				result = new String(Base64.getEncoder().encodeToString(inputByte));
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
//		System.out.println(String.format("result:%s",result));
        return result;
	}
	
	public static String base64Encoding(String message){
		String result = null;	
		if(!StringUtil.isEmptyOrWhitespace(message)){
			message = message.trim();
			result = base64Encoding(message.getBytes());
		}
//		System.out.println(String.format("result:%s",result));
        return result; 
	}
	
	public static String base64Decoding(String base64Message){
		String result = null;	
		if(!StringUtil.isEmptyOrWhitespace(base64Message)){
			base64Message = base64Message.trim();
			try {
				result = new String(Base64.getDecoder().decode(base64Message.getBytes()));
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
//		System.out.println(String.format("result:%s",result));
        return result; 
	}
	
	
}
